﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NWN.GDY.Service.Entity;
using NWN.GDY.Service.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Data.Common;

namespace NWN.GDY.Service.DataAccess
{
    public class RoomDA
    {
        public List<Room> GetRooms(int RoomType)
        {
            List<Room> RoomList = new List<Room>();
            Database db = DatabaseFactory.CreateDatabase("GDYConnectionString");
            //string sqlCommand = "select * from tb_room where RoomType=@roomtype order by SortID";
            string sqlCommand = "select *,(Case RoomType when 0 then '新手房' when 10 then '玩家专房' end) as RoomTypeDesc,(Case isnull(passport,'') when '' then 0 else 1 end) as IsLock from whydb2012.tb_room order by SortID";
            DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
            //db.AddInParameter(dbCommand, "@roomtype", DbType.Int32, RoomType);
            using (IDataReader dr = db.ExecuteReader(dbCommand))
            {
                while (dr.Read())
                {
                    Room room = new Room();
                    room.RoomGUID = DBHelper.GetGuid(dr, "RoomGUID").ToString();
                    room.RoomName = DBHelper.GetString(dr, "RoomName");
                    room.RoomID = DBHelper.GetInt32(dr, "RoomID");
                    room.PlayerCount = DBHelper.GetInt32(dr, "PlayerCount");
                    room.WatcherCount = DBHelper.GetInt32(dr, "WatcherCount");
                    room.RoomTypeDesc = DBHelper.GetString(dr, "RoomTypeDesc");
                    room.IsLock = DBHelper.GetInt(dr, "IsLock");
                    RoomList.Add(room);
                }
            }
            return RoomList;
        }

        public Room GetRoom(string RoomGUID)
        {
            Room room = null;
            Database db = DatabaseFactory.CreateDatabase("GDYConnectionString");
            string sqlCommand = "select * from tb_room where RoomGUID=@RoomGUID";
            DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
            db.AddInParameter(dbCommand, "@RoomGUID", DbType.String, RoomGUID);
            using (IDataReader dr = db.ExecuteReader(dbCommand))
            {
                if (dr.Read())
                {
                    room = new Room();
                    room.RoomGUID = DBHelper.GetGuid(dr, "RoomGUID").ToString();
                    room.RoomName = DBHelper.GetString(dr, "RoomName");
                    room.RoomID = DBHelper.GetInt32(dr, "RoomID");
                    room.PlayerCount = DBHelper.GetInt32(dr, "PlayerCount");
                    room.WatcherCount = DBHelper.GetInt32(dr, "WatcherCount");
                }
            }
            return room;
        }

        public Room GetRoom(string RoomGUID, string userid, long recordid, string vplayrecord, string vroom, string vround, string vucoin, string vuroom, string vuinfo, long chatid, string vchat, string vadminroom, string vroundresult)
        {
            Room room = null;
            Database db = DatabaseFactory.CreateDatabase("GDYConnectionString");
            DbCommand dbCommand = db.GetStoredProcCommand("P_GetRoom");
            db.AddParameter(dbCommand, "@RETURN_VALUE", DbType.Int32, ParameterDirection.ReturnValue, null, DataRowVersion.Default, null);
            db.AddInParameter(dbCommand, "@RoomGUID", DbType.String, RoomGUID);
            db.AddInParameter(dbCommand, "@UserID", DbType.String, userid);
            db.AddInParameter(dbCommand, "@operatortime", DbType.DateTime, DateTime.Now);
            db.AddInParameter(dbCommand, "@TBPLAYRECORDNow", DbType.String, vplayrecord);
            db.AddInParameter(dbCommand, "@TBROOMNow", DbType.String, vroom);
            db.AddInParameter(dbCommand, "@TBROUNDNow", DbType.String, vround);
            db.AddInParameter(dbCommand, "@TBUSERCOINMAPNow", DbType.String, vucoin);
            db.AddInParameter(dbCommand, "@TBUSERROOMMAPNow", DbType.String, vuroom);
            db.AddInParameter(dbCommand, "@TBUSERINFONow", DbType.String, vuinfo);
            db.AddInParameter(dbCommand, "@RecordID", DbType.Int64, recordid);
            db.AddInParameter(dbCommand, "@ChatID", DbType.Int64, chatid);
            db.AddInParameter(dbCommand, "@TBCHATRECORDNow", DbType.String, vchat);
            db.AddInParameter(dbCommand, "@TBADMINROOMMAPNow", DbType.String, vadminroom);
            db.AddInParameter(dbCommand, "@TBROUNDRESULTNow", DbType.String, vroundresult);
            using (IDataReader dr = db.ExecuteReader(dbCommand))
            {
                //顺序为--Room--Coin--Round--playrecord--userroommap--chat--version
                room = new Room();
                if (dr.Read())
                {
                    room.ServerTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    if (dr.FieldCount > 1 && dr[0] != System.DBNull.Value)
                    {
                        room.RoomGUID = RoomGUID;
                        room.RoomName = DBHelper.GetString(dr, "RoomName");
                        room.RoomType = DBHelper.GetInt32(dr, "RoomType");
                        room.GameProcessing = DBHelper.GetBoolean(dr, "GameProcessing");
                        room.PlayerCount = DBHelper.GetInt32(dr, "playercount");
                        room.WatcherCount = DBHelper.GetInt32(dr, "watchercount");
                        room.RoundCoin = DBHelper.GetInt32(dr, "RoundCoin");
                        room.RoundMaxCoin = DBHelper.GetInt32(dr, "RoundMaxCoin");
                        room.CoinID = DBHelper.GetInt32(dr, "CoinID");
                        room.round = new Round();
                        room.round.RoundID = DBHelper.GetGuid(dr, "RoundGUID").ToString();
                        room.round.Refresh = 0;
                    }
                    dr.NextResult();
                    if (dr.Read())
                    {
                        if (dr.FieldCount > 1 && dr[0] != System.DBNull.Value)
                        {
                            if (room.round == null)
                                room.round = new Round();
                            room.round.Refresh = 1;
                            room.round.CardCount = DBHelper.GetInt32(dr, "CardCount");
                            room.round.CurrentPlayer = DBHelper.GetString(dr, "CurrentPlayer");
                            room.round.EndRecordID = DBHelper.GetInt32(dr, "EndRecordID");
                            room.round.LastArrageCardID = DBHelper.GetString(dr, "LastArrageCardID");
                            room.round.LastCardPlayer = DBHelper.GetString(dr, "LastCardPlayer");
                            room.round.LastPlayerTime = DBHelper.GetDateTime(dr, "LastPlayerTime").ToString("yyyy-MM-dd hh:mm:ss");
                            room.round.RoundID = DBHelper.GetGuid(dr, "RoundGUID").ToString();
                            room.round.UpdateTime = DBHelper.GetDateTime(dr, "UpdateTime").ToString("yyyy-MM-dd hh:mm:ss");
                            room.round.RoundCoin = DBHelper.GetInt32(dr, "RoundCoin");
                            room.round.ScoreTimes = DBHelper.GetInt32(dr, "ScoreTimes");
                            room.round.LastType = DBHelper.GetInt32(dr, "LastType");
                            room.round.Status = DBHelper.GetInt32(dr, "Status");
                            room.round.EndTime = DBHelper.GetDateTime(dr, "EndTime").ToString("yyyy-MM-dd hh:mm:ss");
                            room.round.LastTime = DBHelper.GetDateTime(dr, "LastTime").ToString("yyyy-MM-dd hh:mm:ss");
                            room.round.CurrentNickName = DBHelper.GetString(dr, "CurrentNickName");
                        }
                    }
                    dr.NextResult();
                    room.PlayRecordList = new List<PlayRecord>();
                    while (dr.Read())
                    {
                        if (dr[0] != System.DBNull.Value)
                        {
                            PlayRecord pr = new PlayRecord();
                            pr.InsertTime = DBHelper.GetDateTime(dr, "InsertTime").ToString("yyyy-MM-dd hh:mm:ss");
                            pr.NickName = DBHelper.GetString(dr, "NickName");
                            pr.CardID = DBHelper.GetString(dr, "CardID");
                            pr.OperateType = DBHelper.GetInt32(dr, "OperateType");
                            pr.RecordID = DBHelper.GetInt64(dr, "RecordID");
                            pr.RoomGUID = RoomGUID;
                            pr.RoundGUID = DBHelper.GetGuid(dr, "RoundGUID").ToString();
                            pr.SeatID = DBHelper.GetInt32(dr, "SeatID");
                            pr.UserID = DBHelper.GetString(dr, "UserID");
                            pr.CardDesc = DBHelper.GetString(dr, "CardDesc");
                            pr.HandCardType = DBHelper.GetInt32(dr, "HandCardType");
                            room.PlayRecordList.Add(pr);
                        }
                    }
                    dr.NextResult();
                    room.Players = new List<Player>();
                    while (dr.Read())
                    {
                        if (dr[0] != System.DBNull.Value)
                        {
                            Player p = new Player();
                            p.HaveHeadImg = DBHelper.GetBoolean(dr, "HaveHeadImg");
                            p.LeftCardID = DBHelper.GetString(dr, "LeftCardID");
                            p.CardNum = DBHelper.GetInt32(dr, "CardNum");
                            p.NickName = DBHelper.GetString(dr, "NickName");
                            p.SeatID = DBHelper.GetInt32(dr, "SeatID");
                            p.Status = DBHelper.GetInt32(dr, "Status");
                            p.NetStatus = DBHelper.GetInt32(dr, "NetStatus");
                            p.UserID = DBHelper.GetString(dr, "UserID");
                            p.UserScore = DBHelper.GetInt32(dr, "UserScore");
                            p.CoinCount = DBHelper.GetInt32(dr, "CoinCount");
                            p.StatusDesc = DBHelper.GetString(dr, "StatusDesc");
                            p.CoinName = DBHelper.GetString(dr, "CoinName");
                            room.Players.Add(p);
                        }
                    }
                    dr.NextResult();
                    room.ChatRecordList = new List<ChatRecord>();
                    while (dr.Read())
                    {
                        if (dr[0] != System.DBNull.Value)
                        {
                            ChatRecord cr = new ChatRecord();
                            cr.ChatID = DBHelper.GetInt64(dr, "ChatID");
                            cr.UserID = DBHelper.GetString(dr, "UserID");
                            cr.ChatContent = DBHelper.GetString(dr, "ChatContent");
                            //cr.InsertTime = DBHelper.GetDateTime(dr, "InsertTime");
                            cr.NickName = DBHelper.GetString(dr, "NickName");
                            room.ChatRecordList.Add(cr);
                        }
                    }
                    dr.NextResult();
                    if (dr.Read())
                    {
                        if (dr[0] != System.DBNull.Value)
                        {
                            //是管理员
                            room.AdminStatus = 1;
                        }
                        else
                        {
                            //未变化
                            room.AdminStatus = 2;
                        }
                    }
                    else
                        //不是管理员
                        room.AdminStatus = 0;
                    dr.NextResult();
                    room.RoundResultList = new List<RoundResult>();
                    while (dr.Read())
                    {
                        if (dr[0] != System.DBNull.Value)
                        {
                            RoundResult rr = new RoundResult();
                            rr.Coin = DBHelper.GetInt32(dr, "Coin");
                            rr.IsWin = DBHelper.GetBoolean(dr, "IsWin");
                            rr.Score = DBHelper.GetInt32(dr, "Score");
                            rr.UserID = DBHelper.GetString(dr, "UserID");
                            rr.NickName = DBHelper.GetString(dr, "NickName");
                            room.RoundResultList.Add(rr);
                        }
                    }
                    dr.NextResult();
                    dr.Read();
                    room.RoomVersion = new VersionInfo();
                    room.RoomVersion.vadminroom = DBHelper.GetGuid(dr, "TBADMINROOMMAP").ToString();
                    room.RoomVersion.vchat = DBHelper.GetGuid(dr, "TBCHATRECORD").ToString();
                    room.RoomVersion.vplayrecord = DBHelper.GetGuid(dr, "TBPLAYRECORD").ToString();
                    room.RoomVersion.vroom = DBHelper.GetGuid(dr, "TBROOM").ToString();
                    room.RoomVersion.vround = DBHelper.GetGuid(dr, "TBROUND").ToString();
                    room.RoomVersion.vucoin = DBHelper.GetGuid(dr, "TBUSERCOINMAP").ToString();
                    room.RoomVersion.vuinfo = DBHelper.GetGuid(dr, "TBUSERINFO").ToString();
                    room.RoomVersion.vuroom = DBHelper.GetGuid(dr, "TBUSERROOMMAP").ToString();
                    room.RoomVersion.vroundresult = DBHelper.GetGuid(dr, "TBROUNDRESULT").ToString();
                }
            }
            room.Result = (int)dbCommand.Parameters["@RETURN_VALUE"].Value;            
            return room;
        }

        public int EnterRoom(string uid, string passport,string roomguid)
        {
            int iOut = GDYConsts.ReturnDefine.SUCCESS;
            Database db = DatabaseFactory.CreateDatabase("GDYConnectionString");
            DbCommand dbCommand = db.GetStoredProcCommand("P_EnterRoom");
            db.AddParameter(dbCommand, "@RETURN_VALUE", DbType.Int32, ParameterDirection.ReturnValue, null, DataRowVersion.Default, null);
            db.AddInParameter(dbCommand, "@UserID", DbType.String, uid);
            db.AddInParameter(dbCommand, "@RoomGUID", DbType.String, roomguid);
            db.AddInParameter(dbCommand, "@Passport", DbType.String, passport);
            db.AddInParameter(dbCommand, "@operatortime", DbType.DateTime, DateTime.Now);
            db.ExecuteNonQuery(dbCommand);
            iOut = (int)dbCommand.Parameters["@RETURN_VALUE"].Value;
            return iOut;
        }

        public int Sit(string uid, string roomguid,int SeatID)
        {
            int iOut = GDYConsts.ReturnDefine.SUCCESS;
            Database db = DatabaseFactory.CreateDatabase("GDYConnectionString");
            DbCommand dbCommand = db.GetStoredProcCommand("P_Sit");
            db.AddParameter(dbCommand, "@RETURN_VALUE", DbType.Int32, ParameterDirection.ReturnValue, null, DataRowVersion.Default, null);
            db.AddInParameter(dbCommand, "@UserID", DbType.String, uid);
            db.AddInParameter(dbCommand, "@RoomGUID", DbType.String, roomguid);
            db.AddInParameter(dbCommand, "@SeatID", DbType.Int32, SeatID);
            db.AddInParameter(dbCommand, "@operatortime", DbType.DateTime, DateTime.Now);
            db.ExecuteNonQuery(dbCommand);
            iOut = (int)dbCommand.Parameters["@RETURN_VALUE"].Value;
            return iOut;
        }

        public int Stand(string uid, string roomguid)
        {
            int iOut = GDYConsts.ReturnDefine.SUCCESS;
            Database db = DatabaseFactory.CreateDatabase("GDYConnectionString");
            DbCommand dbCommand = db.GetStoredProcCommand("P_Stand");
            db.AddParameter(dbCommand, "@RETURN_VALUE", DbType.Int32, ParameterDirection.ReturnValue, null, DataRowVersion.Default, null);
            db.AddInParameter(dbCommand, "@UserID", DbType.String, uid);
            db.AddInParameter(dbCommand, "@RoomGUID", DbType.String, roomguid);
            db.AddInParameter(dbCommand, "@operatortime", DbType.DateTime, DateTime.Now);
            db.ExecuteNonQuery(dbCommand);
            iOut = (int)dbCommand.Parameters["@RETURN_VALUE"].Value;
            return iOut;
        }

        public int Ready(string uid, string roomguid)
        {
            int iOut = GDYConsts.ReturnDefine.SUCCESS;
            Database db = DatabaseFactory.CreateDatabase("GDYConnectionString");
            DbCommand dbCommand = db.GetStoredProcCommand("P_Ready");
            db.AddParameter(dbCommand, "@RETURN_VALUE", DbType.Int32, ParameterDirection.ReturnValue, null, DataRowVersion.Default, null);
            db.AddInParameter(dbCommand, "@UserID", DbType.String, uid);
            db.AddInParameter(dbCommand, "@RoomGUID", DbType.String, roomguid);
            db.AddInParameter(dbCommand, "@operatortime", DbType.DateTime, DateTime.Now);
            db.ExecuteNonQuery(dbCommand);
            iOut = (int)dbCommand.Parameters["@RETURN_VALUE"].Value;
            return iOut;
        }

        public List<Card> GetCardsInfo()
        {
            List<Card> cardlist = new List<Card>();
            Database db = DatabaseFactory.CreateDatabase("GDYConnectionString");
            //string sqlCommand = "select * from tb_room where RoomType=@roomtype order by SortID";
            string sqlCommand = "select * from whydb2012.tb_card order by cardid";
            DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
            //db.AddInParameter(dbCommand, "@roomtype", DbType.Int32, RoomType);
            using (IDataReader dr = db.ExecuteReader(dbCommand))
            {
                while (dr.Read())
                {
                    Card card = new Card();
                    card.CardID = DBHelper.GetInt32(dr, "CardID");
                    card.CardName = DBHelper.GetString(dr, "CardName");
                    card.CardNum = DBHelper.GetInt32(dr, "CardNum");
                    card.CardType = DBHelper.GetInt32(dr, "CardType");
                    card.SimpleName = DBHelper.GetString(dr, "SimpleName");
                    cardlist.Add(card);
                }
            }
            return cardlist;
        }

        public int Discard(string UserID, string CardID, int handcardtype, string ArrageCardID, string roomGUID, string RoundGUID, string lastcardids, int lasttype)
        {
            int iOut = GDYConsts.ReturnDefine.SUCCESS;
            Database db = DatabaseFactory.CreateDatabase("GDYConnectionString");
            DbCommand dbCommand = db.GetStoredProcCommand("P_Discard");
            db.AddParameter(dbCommand, "@RETURN_VALUE", DbType.Int32, ParameterDirection.ReturnValue, null, DataRowVersion.Default, null);
            db.AddInParameter(dbCommand, "@UserID", DbType.String, UserID);
            db.AddInParameter(dbCommand, "@CardID", DbType.String, CardID);
            db.AddInParameter(dbCommand, "@LastCardID", DbType.String, lastcardids);
            db.AddInParameter(dbCommand, "@handcardtype", DbType.Int32, handcardtype);
            db.AddInParameter(dbCommand, "@ArrageCardID", DbType.String, ArrageCardID);
            db.AddInParameter(dbCommand, "@RoomGUID", DbType.String, roomGUID);
            db.AddInParameter(dbCommand, "@RoundGUID", DbType.String, RoundGUID);
            db.AddInParameter(dbCommand, "@operatortime", DbType.DateTime, DateTime.Now);
            db.AddInParameter(dbCommand, "@LasthandcardType", DbType.Int32, lasttype);
            db.ExecuteNonQuery(dbCommand);
            iOut = (int)dbCommand.Parameters["@RETURN_VALUE"].Value;
            return iOut;
        }

        public int Pass(string uid, string roomguid,string roundguid)
        {
            int iOut = GDYConsts.ReturnDefine.SUCCESS;
            Database db = DatabaseFactory.CreateDatabase("GDYConnectionString");
            DbCommand dbCommand = db.GetStoredProcCommand("P_Pass");
            db.AddParameter(dbCommand, "@RETURN_VALUE", DbType.Int32, ParameterDirection.ReturnValue, null, DataRowVersion.Default, null);
            db.AddInParameter(dbCommand, "@UserID", DbType.String, uid);
            db.AddInParameter(dbCommand, "@RoomGUID", DbType.String, roomguid);
            db.AddInParameter(dbCommand, "@RoundGUID", DbType.String, roundguid);
            db.AddInParameter(dbCommand, "@operatortime", DbType.DateTime, DateTime.Now);
            db.ExecuteNonQuery(dbCommand);
            iOut = (int)dbCommand.Parameters["@RETURN_VALUE"].Value;
            return iOut;
        }

        public int Chat(string uid, string roomguid, string chatcontent)
        {
            int iOut = GDYConsts.ReturnDefine.SUCCESS;
            Database db = DatabaseFactory.CreateDatabase("GDYConnectionString");
            DbCommand dbCommand = db.GetStoredProcCommand("P_Chat");
            db.AddParameter(dbCommand, "@RETURN_VALUE", DbType.Int32, ParameterDirection.ReturnValue, null, DataRowVersion.Default, null);
            db.AddInParameter(dbCommand, "@UserID", DbType.String, uid);
            db.AddInParameter(dbCommand, "@RoomGUID", DbType.String, roomguid);
            db.AddInParameter(dbCommand, "@ChatContent", DbType.String, chatcontent);
            db.AddInParameter(dbCommand, "@operatortime", DbType.DateTime, DateTime.Now);
            db.ExecuteNonQuery(dbCommand);
            iOut = (int)dbCommand.Parameters["@RETURN_VALUE"].Value;
            return iOut;
        }

        public string GetCoinName(int CoinID)
        { 
            Database db = DatabaseFactory.CreateDatabase("GDYConnectionString");
            DbCommand dbCommand = db.GetSqlStringCommand("select CoinName from tb_coin where coinid=@coinid");
            db.AddInParameter(dbCommand, "@coinid", DbType.Int32, CoinID);
            using (IDataReader dr = db.ExecuteReader(dbCommand))
            {
                if (dr.Read())
                {
                    return DBHelper.GetString(dr, "CoinName");
                }
            }
            return "";
        }
    }
}